package com.eastcode.mobile.server.system.action;

import java.util.List;

import com.eastcode.base.action.BaseAction;
import com.eastcode.base.util.DateUtil;
import com.eastcode.mobile.server.system.entity.StationRule;
import com.eastcode.mobile.server.system.service.StationRuleService;
import com.eastcode.mobile.server.system.view.StationRuleView;
import com.eastcode.mobile.server.util.ServerLocator;

public class StationRuleAction extends BaseAction {

	private static final long serialVersionUID = -5391649022188869L;

	private StationRuleService stationRuleService = ServerLocator.getStationRuleService();

	public void execute() {
		prepare(this);
	}

	/**
	 * 查询数据
	 * 
	 */
	public void search() {
		StationRuleView view = parseRequest(StationRuleView.class, "view.");

		List<StationRule> result = stationRuleService.searchList(StationRule.class, view);
		view.setResult(result);

		request.setAttribute("view", view);

		forward("/template/server/stationRule/list.ftl");
	}

	/**
	 * 插入数据
	 * 
	 */
	public void insert() {
		StationRuleView view = parseRequest(StationRuleView.class, "view.");
		request.setAttribute("view", view);

		forward("/template/server/stationRule/input.ftl");
	}

	/**
	 * 保存数据：新增或修改
	 * 
	 * @return
	 */
	public void save() {
		StationRule stationRule = parseRequest(StationRule.class, "");

		long id = Long.parseLong(request.getParameter("view.id"));

		// 处理实体的Key值
		if (id != 0) {
			StationRule old = stationRuleService.findObjectByKey(StationRule.class, id);
			stationRule.setId(old.getId());
			stationRule.setUpdateTime(DateUtil.getToday());
		} else {
			stationRule.setCreateTime(DateUtil.getToday());
		}

		stationRuleService.save(stationRule);

		setResult(stationRule);

		StationRuleView view = parseRequest(StationRuleView.class, "view.");

		request.setAttribute("view", view);

		forward("/template/server/stationRule/detail.ftl");
	}

	/**
	 * 禁用数据
	 * 
	 * @return
	 */
	public void disable() {
		StationRule stationRule = parseRequest(StationRule.class, "");

		long id = Long.parseLong(request.getParameter("view.id"));

		stationRule = stationRuleService.findObjectByKey(StationRule.class, id);

		// 更新数据
		stationRule.setUpdateTime(DateUtil.getToday());

		stationRuleService.save(stationRule);

		search();
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public void delete() {
		long id = Long.parseLong(request.getParameter("view.id"));
		stationRuleService.deleteObjectByKey(StationRule.class, id);
		search();
	}

	/**
	 * 更新数据
	 * 
	 */
	public void update() {

		long id = Long.parseLong(request.getParameter("view.id"));
		StationRule stationRule = stationRuleService.findObjectByKey(StationRule.class, id);
		setResult(stationRule);

		StationRuleView view = parseRequest(StationRuleView.class, "view.");
		view.setId(id);
		request.setAttribute("view", view);

		forward("/template/server/stationRule/input.ftl");
	}

	/**
	 * 查看详情
	 * 
	 */
	public void detail() {
		long id = Long.parseLong(request.getParameter("view.id"));
		StationRule stationRule = stationRuleService.findObjectByKey(StationRule.class, id);
		setResult(stationRule);

		StationRuleView view = parseRequest(StationRuleView.class, "view.");
		view.setId(id);
		request.setAttribute("view", view);

		forward("/template/server/stationRule/detail.ftl");
	}
}